package com.dsbridge.enrollment.dao;

import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.stereotype.Repository;

import com.dsbridge.enrollment.dao.mappers.SubjectsRowMapper;
import com.dsbridge.enrollment.model.SubjectsDTO;

@Repository("subjectsDAO")
public class SubjectsDAO {

	// SimpleJDBCTemplate and SimpleJDBCInsert
	private SimpleJdbcTemplate simpleJdbcTemplate;

	@Autowired
	private DataSource dataSource;

	// Insert
	// Update
	// Read
	
	/**
	 * This method will be used to Create the Suject Records..
	 * @param subject
	 * @return Returns the SubjectID.
	 */
	public int create(SubjectsDTO subject) {
		simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
		String insertSQL = "INSERT INTO subjects(subject_name) VALUES (?)";
		int i = simpleJdbcTemplate.update(insertSQL, subject.getSubjectName());
		return i;		
	}
	
	//Retrieve the Data.
	// Row Mappers in the Spring Framework.
	public List<SubjectsDTO> retrieveAllSubjects(){
		//Each row will be represented as one SubjectsDTO Object.
		//Collection of SubjectsDTO objects - List<SubjectsDTO>
		simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
		String sql="select * from subjects";
		return simpleJdbcTemplate.query(sql, new SubjectsRowMapper());
	}
}
